from __future__ import absolute_import

from . import models
from django.contrib.markup.templatetags.markup import restructuredtext
from django.shortcuts import get_object_or_404, render_to_response
from django.utils.safestring import mark_safe


def plainpage(request, slug):
    page = get_object_or_404(models.PlainPage, slug__exact=slug)

    if page.format == 'html':
        page.title = mark_safe(page.title)
        page.content = mark_safe(page.content)
    elif page.format == 'restructuredtext':
        page.title = restructuredtext(page.title)
        page.content = restructuredtext(page.content)
    else:
        # XXX Uh-oh. Should never happen.
        raise RuntimeError("plain page in some unknown format")

    return render_to_response('plainpages/default.html', {'page': page})
